package top.shiyiri.servlet;

import top.shiyiri.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Aunean
 * @date 2022/2/9 11:45
 */
public class CookieServlet extends BaseServlet {

    /**
     * 创建cookie
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void createCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie cookie = new Cookie("key1", "value1");
        //2.通知客户端保存cookie
        response.addCookie(cookie);
        //1.创建cookie对象
        Cookie cookie2 = new Cookie("key2", "value2");
        //2.通知客户端保存cookie
        response.addCookie(cookie2);
        response.getWriter().write("Cookie创建成功");

    }

    /**
     * 获取cookie
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cookie[] cookies = request.getCookies();

        for (Cookie cookie : cookies) {
            response.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "] <br/>");
        }

        Cookie iWantCookie = CookieUtils.findCookie("key2", cookies);
        //如果不等于null，说明赋过值，找到了需要的Cookie
        if (iWantCookie != null) {
            response.getWriter().write("找到了需要的Cookie");
        }
    }

    /**
     * 修改Cookie
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*        //方案一：
        //1.创建一个要修改的同名的Cookie对象
        //2.在构造器赋予新的Cookie值
        Cookie cookie = new Cookie("key1", "newValue1");
        //3.调用response.addCookie(Cookie);
        response.addCookie(cookie);
        response.getWriter().write("key1的Cookie已经修改好");*/

        //方案二：
        //1.先查找到需要修改的Cookie对象
        Cookie cookie = CookieUtils.findCookie("key2", request.getCookies());
        //2.调用setValue()方法赋予新的Cookie值
        if (cookie != null) {
            cookie.setValue("newValue2");
        }
        //3.调用response.addCookie()通知客户端保存修改
        response.addCookie(cookie);
        response.getWriter().write("key2的Cookie已经修改好");
    }

    /**
     * Cookie默认的生命周期
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void defaultLife(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultLifeValue");

        cookie.setMaxAge(-1);//设置存活时间
        response.addCookie(cookie);
    }

    protected void deleteNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //先找到你要删除的Cookie对象
        Cookie cookie = CookieUtils.findCookie("key1", request.getCookies());
        if (cookie != null) {
            //调用 setMaxAge(0)
            cookie.setMaxAge(0);//表示马上删除，不需要等待浏览器关闭
            //调用 response.addCookie(cookie)
            response.addCookie(cookie);
            response.getWriter().write("key1的Cookie已经被删除");
        }
    }

    protected void life3600(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600", "life3600");

        cookie.setMaxAge(60 * 60);//设置生命周期为一小时

        response.addCookie(cookie);

        response.getWriter().write("已经创建了一个存活一小时的Cookie");
    }

    protected void testPath(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("path1", "path1");
        //getContextPath() --> 得到工程路径
        System.out.println(request.getContextPath());
        cookie.setPath(request.getContextPath() + "/abc");
        response.addCookie(cookie);
        response.getWriter().write("创建了一个带有Path路径的Cookie");
    }
}